﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class MainPanel : MonoBehaviour
{
    private Button btn_Start;
    private Button btn_Quit;

    private Button btn_Shop;
    private Button btn_Rank;
    private Button btn_Sound;
    private Button btn_Reset;

    private ManagerVars vars;

    private void Awake()
    {
        //广播事件码--主页
        EventCenter.AddListener(EventDefine.ShowMainPanel, Show);
        EventCenter.AddListener<int>(EventDefine.ChangeSkin,ChangeSkin);
        Init();
        vars = ManagerVars.GetManagerVars();
    }
    private void Show()
    {
        gameObject.SetActive(true);
    }
    private void Start()
    {
        if (GameData.isAgainGame)
        {
            //开始游戏
            EventCenter.Broadcast(EventDefine.ShowGamePanel);
            gameObject.SetActive(false);//当前界面隐藏
        }
        //检查当前ui皮肤
        ChangeSkin(GameManager.Instance.GetSelectedSkin());
        Sound();
    }
    private void Update()
    {
        //匹配手机上的返回键
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            //退出app，，（发布出来才好用）
            Application.Quit();
        }
    }
    private void Init()
    {
        btn_Start = transform.Find("btn_Start").GetComponent<Button>();
        btn_Start.onClick.AddListener(OnStartButtonClick);
        btn_Quit = transform.Find("btn_Quit").GetComponent<Button>();
        btn_Quit.onClick.AddListener(OnQuitButtonClick);

        btn_Shop = transform.Find("btns/btn_Shop").GetComponent<Button>();
        btn_Shop.onClick.AddListener(OnShopButtonClick);
        btn_Reset = transform.Find("btns/btn_Reset").GetComponent<Button>();
        btn_Reset.onClick.AddListener(OnResetButtonClick);
        btn_Rank = transform.Find("btns/btn_Rank").GetComponent<Button>();
        btn_Rank.onClick.AddListener(OnRankButtonClick);
        btn_Sound = transform.Find("btns/btn_Sound").GetComponent<Button>();
        btn_Sound.onClick.AddListener(OnSoundButtonClick);
    }
    /// <summary>
    /// 皮肤更换，更换UI图片
    /// </summary>
    /// <param name="SkinIndex"></param>
    private void ChangeSkin(int SkinIndex)
    {
        btn_Shop.transform.GetChild(0).GetComponent<Image>().sprite=vars.skinSpriteList[SkinIndex];//显示在主页的人物替换
    }

    #region 监听事件点击的方法
    /// <summary>
    /// 开始按钮的点击
    /// </summary>
    private void OnStartButtonClick()
    { 
        //广播事件码--音效
        EventCenter.Broadcast(EventDefine.PlayClickAudio);
        GameManager.Instance.IsGameStarted = true;
        //广播事件码--游戏面板
        EventCenter.Broadcast(EventDefine.ShowGamePanel);
        //自身界面隐藏
        gameObject.SetActive(false);
    }
    /// <summary>
    /// 退出按钮的点击
    /// </summary>
    private void OnQuitButtonClick()
    {
        Application.Quit();
    }
    /// <summary>
    /// 商店按钮的点击
    /// </summary>
    private void OnShopButtonClick()
    {
        //添加监听--商城
        EventCenter.Broadcast(EventDefine.ShowShopPanel);
        //广播事件码--音效
        EventCenter.Broadcast(EventDefine.PlayClickAudio);
        gameObject.SetActive(false);
    }
    /// <summary>
    /// 重置按钮的点击
    /// </summary>
    private void OnResetButtonClick()
    {
        //添加监听--重置
        EventCenter.Broadcast(EventDefine.ShowResetPanel);
        //广播事件码--音效
        EventCenter.Broadcast(EventDefine.PlayClickAudio);
        //gameObject.SetActive(false);不跳转页面
    }
    /// <summary>
    /// 排行榜按钮的点击
    /// </summary>
    private void OnRankButtonClick()
    {
        //广播事件码--音效
        EventCenter.Broadcast(EventDefine.ShowRankPanel);
        //广播事件码--音效
        EventCenter.Broadcast(EventDefine.PlayClickAudio);
        //gameObject.SetActive(false);
    }
    /// <summary>
    /// 音效按钮的点击
    /// </summary>
    private void OnSoundButtonClick()
    {
        //广播事件码--音效
        EventCenter.Broadcast(EventDefine.PlayClickAudio);
        //设置一个相反的 - 开关
        GameManager.Instance.SetIsMusicOn(!GameManager.Instance.GetIsMusicOn());
        Sound();
    }
    /// <summary>
    /// 设置声音
    /// </summary>
    private void Sound() 
    {
         //图片
        if (GameManager.Instance.GetIsMusicOn())
            //开启
        {
            btn_Sound.transform.GetChild(0).GetComponent<Image>().sprite = vars.isMusicOn;
        }
        else
        {
            btn_Sound.transform.GetChild(0).GetComponent<Image>().sprite = vars.isMusicOff;
        }
        //广播 -音效是否开
        EventCenter.Broadcast(EventDefine.IsMusicOn,GameManager.Instance.GetIsMusicOn());
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventDefine.ShowMainPanel,Show);
     //TOTO 忘记添加的移除事件
        EventCenter.RemoveListener<int>(EventDefine.ChangeSkin,ChangeSkin);
    }
    #endregion
}
